package com.su.kddq.service.courier.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.su.kddq.constant.RabbitMqConstant;
import com.su.kddq.constant.TxStateConstant;
import com.su.kddq.entity.courier.CourierOrder;
import com.su.kddq.entity.courier.TxSerial;
import com.su.kddq.entity.user.CourierStatus;
import com.su.kddq.mapper.courier.CourierOrderMapper;
import com.su.kddq.mapper.courier.CourierStatusMapper;
import com.su.kddq.mapper.courier.TxSerialMapper;
import com.su.kddq.productor.OrderProductor;
import com.su.kddq.service.courier.CourierStatusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import sun.misc.FloatingDecimal;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;

/**
 * 取件员状态 service
 *
 * @author sx
 */
@Service
public class CourierStatusServiceImpl extends ServiceImpl<CourierStatusMapper, CourierStatus> implements CourierStatusService {

    @Autowired
    private CourierStatusMapper courierStatusMapper;

    @Autowired
    private CourierOrderMapper courierOrderMapper;

    @Autowired
    private OrderProductor productor;

    @Autowired
    private TxSerialMapper txSerialMapper;


    private static ReentrantLock lock=new ReentrantLock();

    /**
     * 取件员日、月收益
     *
     * @return
     */
    @Override
    public Map getMoneybyDayAndMonth() {
        Map<String, Object> map = new HashMap();
        try {
            String cid = SecurityContextHolder.getContext().getAuthentication().getName();
            Float dayMoney = courierStatusMapper.queryDayMoney(cid);
            Float monthMoney = courierStatusMapper.queryMonthMoney(cid);
            Float allMoney = courierStatusMapper.queryAllMoney(cid);
            map.put("dayMoney", dayMoney);
            map.put("monthMoney", monthMoney);
            map.put("allMoney", allMoney);
            return map;
        } catch (Exception e) {
            log.error(e.getMessage());
            return map;
        }
    }

    @Override
    public boolean MoneyTx() {
        lock.lock();
        try {
            Map msgMap = new HashMap();
            DecimalFormat decimalFormat = new DecimalFormat(".00");
            String cid = SecurityContextHolder.getContext().getAuthentication().getName();
            //更新提现状态--审核中
            courierStatusMapper.update(null, new UpdateWrapper<CourierStatus>()
                    .set("tx_state", TxStateConstant.TX_STATE_IN)
                    .eq("uid", cid));
            List<CourierOrder> courierOrders = courierOrderMapper.MoneyTxByCid(cid);
            double sum = courierOrders.stream().mapToDouble(CourierOrder::getQjyOrderMoney).sum();
            String money = decimalFormat.format(sum);
            //新增提现流水
            TxSerial txSerial = new TxSerial();
            txSerial.setUid(Integer.valueOf(cid));
            txSerial.setCreatTime(new Date());
            txSerial.setTxMoney(money);
            txSerialMapper.insert(txSerial);
            //收集打款订单id集合
            List<String> orderIds = courierOrders.stream().map(CourierOrder::getId).collect(Collectors.toList());
            msgMap.put("cid", cid);
            msgMap.put("orderIds", orderIds);
            msgMap.put("txId",txSerial.getId());
            //发送mq-提现
            productor.SendOrderMes(msgMap, RabbitMqConstant.EXCHANGE_TX, RabbitMqConstant.ROUTING_KEY_TX);
            return true;
        } catch (Exception e) {
            log.error(e.getMessage());
            return false;
        }finally {
            lock.unlock();
        }
    }

    @Override
    public Integer queryTxState() {
        String cid = SecurityContextHolder.getContext().getAuthentication().getName();
        CourierStatus courierStatus = courierStatusMapper.selectOne(new QueryWrapper<CourierStatus>().eq("uid", cid));
        return courierStatus.getTxState();
    }
}
